home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / ov.h < prev    next >
C/C++ Source or Header  |  1997-05-26  |  13KB  |  487 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if !defined (octave_value_h)
  24. #define octave_value_h 1
  25.  
  26. #if defined (__GNUG__)
  27. #pragma interface
  28. #endif
  29.  
  30. #include <cstdlib>
  31.  
  32. #include <string>
  33.  
  34. class ostream;
  35.  
  36. #include "Range.h"
  37. #include "idx-vector.h"
  38. #include "mx-base.h"
  39. #include "oct-alloc.h"
  40. #include "str-vec.h"
  41.  
  42. #include "error.h"
  43. #include "pt-exp.h"
  44.  
  45. class Octave_map;
  46. class octave_value_list;
  47.  
  48. // Constants.
  49.  
  50. // This just provides a way to avoid infinite recursion when building
  51. // octave_value objects.
  52.  
  53. class
  54. octave_xvalue
  55. {
  56. public:
  57.  
  58.   octave_xvalue (void) { }
  59. };
  60.  
  61. class octave_value;
  62.  
  63. // XXX FIXME XXX -- these should probably really be inside the scope
  64. // of the octave_value class, but the cygwin32 beta16 version of g++
  65. // can't handlt that.
  66.  
  67. typedef octave_value (*binary_op_fcn)
  68.   (const octave_value&, const octave_value&);
  69.  
  70. typedef octave_value (*assign_op_fcn)
  71.   (octave_value&, const octave_value_list&, const octave_value&);
  72.  
  73. typedef octave_value * (*type_conv_fcn) (const octave_value&);
  74.  
  75. class
  76. octave_value
  77. {
  78. public:
  79.  
  80.   enum binary_op
  81.   {
  82.     add,
  83.     sub,
  84.     mul,
  85.     div,
  86.     pow,
  87.     ldiv,
  88.     lt,
  89.     le,
  90.     eq,
  91.     ge,
  92.     gt,
  93.     ne,
  94.     el_mul,
  95.     el_div,
  96.     el_pow,
  97.     el_ldiv,
  98.     el_and,
  99.     el_or,
  100.     struct_ref,
  101.     num_binary_ops,
  102.     unknown_binary_op
  103.   };
  104.  
  105.   static string binary_op_as_string (binary_op);
  106.  
  107.   enum magic_colon { magic_colon_t };
  108.   enum all_va_args { all_va_args_t };
  109.  
  110.   octave_value (void);
  111.   octave_value (double d);
  112.   octave_value (const Matrix& m);
  113.   octave_value (const DiagMatrix& d);
  114.   octave_value (const RowVector& v, int pcv = -1);
  115.   octave_value (const ColumnVector& v, int pcv = -1);
  116.   octave_value (const Complex& C);
  117.   octave_value (const ComplexMatrix& m);
  118.   octave_value (const ComplexDiagMatrix& d);
  119.   octave_value (const ComplexRowVector& v, int pcv = -1);
  120.   octave_value (const ComplexColumnVector& v, int pcv = -1);
  121.   octave_value (const char *s);
  122.   octave_value (const string& s);
  123.   octave_value (const string_vector& s);
  124.   octave_value (const charMatrix& chm, bool is_string = false);
  125.   octave_value (double base, double limit, double inc);
  126.   octave_value (const Range& r);
  127.   octave_value (const Octave_map& m);
  128.   octave_value (octave_value::magic_colon);
  129.   octave_value (octave_value::all_va_args);
  130.  
  131.   octave_value (octave_value *new_rep);
  132.  
  133.   // Copy constructor.
  134.  
  135.   octave_value (const octave_value& a)
  136.     {
  137.       rep = a.rep;
  138.       rep->count++;
  139.     }
  140.  
  141.   // Delete the representation of this constant if the count drops to
  142.   // zero.
  143.  
  144.   virtual ~octave_value (void);
  145.  
  146.   // This should only be called for derived types.
  147.  
  148.   virtual octave_value *clone (void) { panic_impossible (); }
  149.  
  150.   void make_unique (void)
  151.     {
  152.       if (rep->count > 1)
  153.     {
  154.       --rep->count;
  155.       rep = rep->clone ();
  156.       rep->count = 1;
  157.     }
  158.     }
  159.  
  160.   void *operator new (size_t size)
  161.     { return allocator.alloc (size); }
  162.  
  163.   void operator delete (void *p, size_t size)
  164.     { allocator.free (p, size); }
  165.  
  166.   // Simple assignment.
  167.  
  168.   octave_value& operator = (const octave_value& a)
  169.     {
  170.       if (rep != a.rep)
  171.     {
  172.       if (--rep->count == 0)
  173.         delete rep;
  174.  
  175.       rep = a.rep;
  176.       rep->count++;
  177.     }
  178.  
  179.       return *this;
  180.     }
  181.  
  182.   virtual type_conv_fcn numeric_conversion_function (void) const
  183.     { return rep->numeric_conversion_function (); }
  184.  
  185.   void maybe_mutate (void);
  186.  
  187.   virtual octave_value *try_narrowing_conversion (void)
  188.     { return rep->try_narrowing_conversion (); }
  189.  
  190.   virtual octave_value index (const octave_value_list& idx) const
  191.     { return rep->index (idx); }
  192.  
  193.   octave_value& assign (const octave_value_list& idx, const octave_value& rhs);
  194.  
  195.   virtual idx_vector index_vector (void) const
  196.     { return rep->index_vector (); }
  197.  
  198.   virtual octave_value
  199.   struct_elt_val (const string& nm, bool silent = false) const
  200.     { return rep->struct_elt_val (nm, silent); }
  201.  
  202.   virtual octave_value& struct_elt_ref (const string& nm)
  203.     { return rep->struct_elt_ref (nm); }
  204.  
  205.   // Size.
  206.  
  207.   virtual int rows (void) const
  208.     { return rep->rows (); }
  209.  
  210.   virtual int columns (void) const
  211.     { return rep->columns (); }
  212.  
  213.   // Does this constant have a type?  Both of these are provided since
  214.   // it is sometimes more natural to write is_undefined() instead of
  215.   // ! is_defined().
  216.  
  217.   virtual bool is_defined (void) const
  218.     { return rep->is_defined (); }
  219.  
  220.   bool is_undefined (void) const
  221.     { return ! is_defined (); }
  222.  
  223.   virtual bool is_real_scalar (void) const
  224.     { return rep->is_real_scalar (); }
  225.  
  226.   virtual bool is_real_matrix (void) const
  227.     { return rep->is_real_matrix (); }
  228.  
  229.   virtual bool is_complex_scalar (void) const
  230.     { return rep->is_complex_scalar (); }
  231.  
  232.   virtual bool is_complex_matrix (void) const
  233.     { return rep->is_complex_matrix (); }
  234.  
  235.   virtual bool is_char_matrix (void) const
  236.     { return rep->is_char_matrix (); }
  237.  
  238.   virtual bool is_string (void) const
  239.     { return rep->is_string (); }
  240.  
  241.   virtual bool is_range (void) const
  242.     { return rep->is_range (); }
  243.  
  244.   virtual bool is_map (void) const
  245.     { return rep->is_map (); }
  246.  
  247.   virtual bool is_magic_colon (void) const
  248.     { return rep->is_magic_colon (); }
  249.  
  250.   virtual bool is_all_va_args (void) const
  251.     { return rep->is_all_va_args (); }
  252.  
  253.   // Are any or all of the elements in this constant nonzero?
  254.  
  255.   virtual octave_value all (void) const
  256.     { return rep->all (); }
  257.  
  258.   virtual octave_value any (void) const
  259.     { return rep->any (); }
  260.  
  261.   // Other type stuff.
  262.  
  263.   virtual bool is_real_type (void) const
  264.     { return rep->is_real_type (); }
  265.  
  266.   virtual bool is_complex_type (void) const
  267.     { return rep->is_complex_type (); }
  268.  
  269.   virtual bool is_scalar_type (void) const
  270.     { return rep->is_scalar_type (); }
  271.  
  272.   virtual bool is_matrix_type (void) const
  273.     { return rep->is_matrix_type (); }
  274.  
  275.   virtual bool is_numeric_type (void) const
  276.     { return rep->is_numeric_type (); }
  277.  
  278.   virtual bool valid_as_scalar_index (void) const
  279.     { return rep->valid_as_scalar_index (); }
  280.  
  281.   virtual bool valid_as_zero_index (void) const
  282.     { return rep->valid_as_zero_index (); }
  283.  
  284.   // Does this constant correspond to a truth value?
  285.  
  286.   virtual bool is_true (void) const
  287.     { return rep->is_true (); }
  288.  
  289.   // Is at least one of the dimensions of this constant zero?
  290.  
  291.   virtual bool is_empty (void) const
  292.     { return rep->is_empty (); }
  293.  
  294.   // Are the dimensions of this constant zero by zero?
  295.  
  296.   virtual bool is_zero_by_zero (void) const
  297.     { return rep->is_zero_by_zero (); }
  298.  
  299.   // Values.
  300.  
  301.   virtual double double_value (bool frc_str_conv = false) const
  302.     { return rep->double_value (frc_str_conv); }
  303.  
  304.   virtual Matrix matrix_value (bool frc_str_conv = false) const
  305.     { return rep->matrix_value (frc_str_conv); }
  306.  
  307.   virtual Complex complex_value (bool frc_str_conv = false) const
  308.     { return rep->complex_value (frc_str_conv); }
  309.  
  310.   virtual ComplexMatrix complex_matrix_value (bool frc_str_conv = false) const
  311.     { return rep->complex_matrix_value (frc_str_conv); }
  312.  
  313.   virtual charMatrix char_matrix_value (bool frc_str_conv = false) const
  314.     { return rep->char_matrix_value (frc_str_conv); }
  315.  
  316.   virtual string_vector all_strings (void) const
  317.     { return rep->all_strings (); }
  318.  
  319.   virtual string string_value (void) const
  320.     { return rep->string_value (); }
  321.  
  322.   virtual Range range_value (void) const
  323.     { return rep->range_value (); }
  324.  
  325.   virtual Octave_map map_value (void) const;
  326.  
  327.   // Unary ops.
  328.  
  329.   virtual octave_value not (void) const { return rep->not (); }
  330.  
  331.   virtual octave_value uminus (void) const { return rep->uminus (); }
  332.  
  333.   virtual octave_value transpose (void) const { return rep->transpose (); }
  334.  
  335.   virtual octave_value hermitian (void) const { return rep->hermitian (); }
  336.  
  337.   virtual void increment (void)
  338.     {
  339.       make_unique ();
  340.       rep->increment ();
  341.     }
  342.  
  343.   virtual void decrement (void)
  344.     {
  345.       make_unique ();
  346.       rep->decrement ();
  347.     }
  348.  
  349.   ColumnVector vector_value (bool frc_str_conv = false,
  350.                  bool frc_vec_conv = false) const;
  351.  
  352.   ComplexColumnVector
  353.   complex_vector_value (bool frc_str_conv = false,
  354.             bool frc_vec_conv = false) const;
  355.  
  356.   // Conversions.  These should probably be private.  If a user of this
  357.   // class wants a certain kind of constant, he should simply ask for
  358.   // it, and we should convert it if possible.
  359.  
  360.   virtual octave_value convert_to_str (void) const
  361.     { return rep->convert_to_str (); }
  362.  
  363.   virtual void convert_to_row_or_column_vector (void)
  364.     { rep->convert_to_row_or_column_vector (); }
  365.  
  366.   void print (bool pr_as_read_syntax = false);
  367.  
  368.   virtual void print (ostream& os, bool pr_as_read_syntax = false)
  369.     { rep->print (os, pr_as_read_syntax); }
  370.  
  371.   void print_with_name (const string& name, bool print_padding = true);
  372.  
  373.   void print_with_name (ostream& os, const string& name,
  374.             bool print_padding = true);
  375.  
  376.   virtual int type_id (void) const { return rep->type_id (); }
  377.  
  378.   virtual string type_name (void) const { return rep->type_name (); }
  379.  
  380.   // Binary and unary operations.
  381.  
  382.   friend octave_value do_binary_op (octave_value::binary_op,
  383.                     const octave_value&,
  384.                     const octave_value&);
  385.  
  386.   // Can we make these go away?
  387.  
  388.   bool print_as_scalar (void);
  389.  
  390. protected:
  391.  
  392.   octave_value (const octave_xvalue&) : rep (0) { }
  393.  
  394. private:
  395.  
  396.   static octave_allocator allocator;
  397.  
  398.   union
  399.     {
  400.       octave_value *rep;      // The real representation.
  401.       int count;              // A reference count.
  402.     };
  403.  
  404.   bool convert_and_assign (const octave_value_list& idx,
  405.                const octave_value& rhs);
  406.  
  407.   bool try_assignment_with_conversion (const octave_value_list& idx,
  408.                        const octave_value& rhs);
  409.  
  410.   bool try_assignment (const octave_value_list& idx,
  411.                const octave_value& rhs);
  412. };
  413.  
  414. // If TRUE, allow assignments like
  415. //
  416. //   octave> A(1) = 3; A(2) = 5
  417. //
  418. // for A already defined and a matrix type.
  419. extern bool Vdo_fortran_indexing;
  420.  
  421. // Should we allow things like:
  422. //
  423. //   octave> 'abc' + 0
  424. //   97 98 99
  425. //
  426. // to happen?  A positive value means yes.  A negative value means
  427. // yes, but print a warning message.  Zero means it should be
  428. // considered an error.
  429. extern int Vimplicit_str_to_num_ok;
  430.  
  431. // Should we allow silent conversion of complex to real when a real
  432. // type is what we're really looking for?  A positive value means yes.
  433. // A negative value means yes, but print a warning message.  Zero
  434. // means it should be considered an error.
  435. extern int Vok_to_lose_imaginary_part;
  436.  
  437. // If TRUE, create column vectors when doing assignments like:
  438. //
  439. //   octave> A(1) = 3; A(2) = 5
  440. //
  441. // (for A undefined).  Only matters when resize_on_range_error is also
  442. // TRUE.
  443. extern bool Vprefer_column_vectors;
  444.  
  445. // If TRUE, prefer logical (zore-one) indexing over normal indexing
  446. // when there is a conflice.  For example, given a = [2, 3], the
  447. // expression  a ([1, 1]) would return [2 3] (instead of [2 2], which
  448. // would be returned if prefer_zero_one_indxing were FALSE).
  449. extern bool Vprefer_zero_one_indexing;
  450.  
  451. // If TRUE, print the name along with the value.
  452. extern bool Vprint_answer_id_name;
  453.  
  454. // Should operations on empty matrices return empty matrices or an
  455. // error?  A positive value means yes.  A negative value means yes,
  456. // but print a warning message.  Zero means it should be considered an
  457. // error.
  458. extern int Vpropagate_empty_matrices;
  459.  
  460. // If TRUE, resize matrices when performing and indexed assignment and
  461. // the indices are outside the current bounds.
  462. extern bool Vresize_on_range_error;
  463.  
  464. // How many levels of structure elements should we print?
  465. extern int Vstruct_levels_to_print;
  466.  
  467. // Allow divide by zero errors to be suppressed.
  468. extern bool Vwarn_divide_by_zero;
  469.  
  470. // Indentation level for structures.
  471. extern int struct_indent;
  472.  
  473. extern void symbols_of_value (void);
  474.  
  475. extern void increment_struct_indent (void);
  476. extern void decrement_struct_indent (void);
  477.  
  478. extern void install_types (void);
  479.  
  480. #endif
  481.  
  482. /*
  483. ;; Local Variables: ***
  484. ;; mode: C++ ***
  485. ;; End: ***
  486. */
  487.